package com.atguigu.stgy.admin.controller;

import com.atguigu.stgy.admin.service.ApartmentInfoService;
import com.atguigu.stgy.model.dto.ApartmentDTO;
import com.atguigu.stgy.model.dto.ApartmentSearchDTO;
import com.atguigu.stgy.model.entity.ApartmentInfo;
import com.atguigu.stgy.model.result.R;
import com.atguigu.stgy.model.vo.ApartmentDetailVO;
import com.atguigu.stgy.model.vo.ApartmentVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/apartment")
public class ApartmentInfoController {

    @Resource
    ApartmentInfoService apartmentInfoService;
    ///admin/apartment/listInfoByDistrictId?id=110113
    @Operation(summary = "根据区id查询公寓列表")
    @GetMapping("listInfoByDistrictId") //请求参数：pojo入参
    public R<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam("id")Long id) {
        List<ApartmentInfo> apartmentInfos = apartmentInfoService.list(Wrappers.lambdaQuery(ApartmentInfo.class)
                .eq(ApartmentInfo::getDistrictId , id));
        return R.ok(apartmentInfos);
    }
    //http://localhost:8080/admin/apartment/getDetailById?id=9
    @Operation(summary = "id查询公寓详情")
    @GetMapping("getDetailById") //请求参数：pojo入参
    public R<ApartmentDetailVO> getDetailById(@RequestParam("id")Long id) {
        ApartmentDetailVO apartmentDetailVO = apartmentInfoService.getDetailById(id);
        return R.ok(apartmentDetailVO);
    }
    ///admin/apartment/updateReleaseStatusById?id=9&status=1
    @Operation(summary = "新增/更新公寓信息")
    @PostMapping("updateReleaseStatusById")
    public R updateReleaseStatusById(@RequestParam("id")Long id , @RequestParam("status")Integer status) {
        apartmentInfoService.update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                .eq(ApartmentInfo::getId , id)
                .set(ApartmentInfo::getIsRelease , status)
                .set(ApartmentInfo::getUpdateTime , new Date()));

        return R.ok();
    }
    @Operation(summary = "条件查询公寓分页列表")
    @GetMapping("pageItem") //请求参数：pojo入参
    public R<Page<ApartmentVO>> pageItem(ApartmentSearchDTO searchDTO) {
        Page<ApartmentVO> page = apartmentInfoService.pageItem(searchDTO);

        return R.ok(page);
    }

    // 新增/更新提交的数据对应了五张表
    //  新创建DTO接受前端提交的复杂数据
    @Operation(summary = "新增/更新公寓信息")
    @PostMapping("saveOrUpdate")
    public R saveOrUpdate(@RequestBody ApartmentDTO apartmentDTO) {
        apartmentInfoService.saveOrUpdateApartmentDTO(apartmentDTO);

        return R.ok();
    }
}
